package com.example.permissionsystem1_1.util.thirdPartyUtil;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 极光推送功能的工具类
 */
public class JPushUtil {

	protected static final Logger LOG = LoggerFactory.getLogger(JPushUtil.class);

	//ios appkey: fff80e6139f0258035fb7655 mk: 779a6c693d9bd405b34fb009
	//android: 92e5f5615e8149af956f459d   12385e533fbb622946d56671
	//5d7db60565b252addf386d15
	//0c466ae255a0ae8f18a9989f
	/** 使用的极光账号的应用对象的key(每个账号唯一) */
	private String APP_KEY = "84fd75a46053dfbcceb0a92b";
	/** 使用的极光账号的密匙(每个账号唯一) */
	private String MASTER_SECRET = "6c084c13daa76dac3e491538";
	private String TAG = "PIS";
	private String MSG_CONTENT = "hello,how are you?";
	private String FROM = "PIS";
	private static JPushUtil instence;

	private JPushClient jpushClient = null;

	private JPushUtil() {
		super();
		ClientConfig clientConfig = ClientConfig.getInstance();
		jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
	}

	public JPushUtil(String MASTER_SECRET, String APP_KEY) {
		super();
		this.MASTER_SECRET = MASTER_SECRET;
		this.APP_KEY = APP_KEY;
		ClientConfig clientConfig = ClientConfig.getInstance();
		jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
	}

	public static JPushUtil getInstence() {
		if (instence == null) {
			synchronized (JPushUtil.class) {
				instence = new JPushUtil();
			}
		}
		return instence;
	}
	
	/**
	 * 根据指定的若干别名来向对应别名的手机app客户端发送app推送消息的方法
	 * @param alias 推送对象的别名
	 * @param message 推送的数据信息
	 * @param from 
	 * @return
	 */
	public boolean publish_alias(String[] alias, String message, String from) {
		ClientConfig clientConfig = ClientConfig.getInstance();
		JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);

		PushPayload payload = buildPushObject_ios_audienceMore_messageWithExtras_alias(alias, message, from);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
			return true;
		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
			LOG.error("Sendno: " + payload.getSendno());
			return false;
		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
			LOG.error("Sendno: " + payload.getSendno());
			return false;
		}
	}

	private static PushPayload buildPushObject_ios_audienceMore_messageWithExtras_alias(String[] alias, String MSG_CONTENT,
			String from) {
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.alias(alias))
				.setNotification(Notification.alert(MSG_CONTENT))
				.setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).addExtra("from", from).build()).build();
	}

	public void publish_tag(String[] tag, String message, String from) {

		PushPayload payload = this.buildPushObject_ios_audienceMore_messageWithExtras_tag(tag, message, from);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
			LOG.info("Got result - " + result);

		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
			LOG.error("Sendno: " + payload.getSendno());

		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
			LOG.error("Sendno: " + payload.getSendno());
		}
	}
	
	/**
	 * 广播(所有人)推送:向所有安装该账号指向的手机app客户端发送app推送消息的方法
	 * @param message 推送数据信息
	 * @param from
	 */
	public void publish(String message, String from) {

		PushPayload payload = this.buildPushObject_ios_audienceMore_messageWithExtras(message, from);
		try {
			PushResult result = jpushClient.sendPush(payload);
			System.out.println(result);
			LOG.info("Got result - " + result);

		} catch (APIConnectionException e) {
			LOG.error("Connection error. Should retry later. ", e);
			LOG.error("Sendno: " + payload.getSendno());

		} catch (APIRequestException e) {
			LOG.error("Error response from JPush server. Should review and fix it. ", e);
			LOG.info("HTTP Status: " + e.getStatus());
			LOG.info("Error Code: " + e.getErrorCode());
			LOG.info("Error Message: " + e.getErrorMessage());
			LOG.info("Msg ID: " + e.getMsgId());
			LOG.error("Sendno: " + payload.getSendno());
		}
	}

	public PushPayload buildPushObject_ios_audienceMore_messageWithExtras_tag(String[] TAG, String MSG_CONTENT, String FROM) {
		/*
		 * return PushPayload.newBuilder() .setPlatform(Platform.android_ios())
		 * .setAudience(Audience.newBuilder()
		 * .addAudienceTarget(AudienceTarget.tag(TAG))
		 * .addAudienceTarget(AudienceTarget.alias("18377613902")) .build())
		 * .setMessage(Message.newBuilder() .setMsgContent(MSG_CONTENT)
		 * .addExtra("from", FROM) .build()) .build();
		 */
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.tag(TAG))
				.setNotification(Notification.alert(MSG_CONTENT))
				.setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).setTitle("title").addExtra("from", FROM)
						.addExtra("message extras key", "extrasparam").build())
				.build();

	}
	
	public PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String MSG_CONTENT, String FROM) {
		/*
		 * return PushPayload.newBuilder() .setPlatform(Platform.android_ios())
		 * .setAudience(Audience.newBuilder()
		 * .addAudienceTarget(AudienceTarget.tag(TAG))
		 * .addAudienceTarget(AudienceTarget.alias("18377613902")) .build())
		 * .setMessage(Message.newBuilder() .setMsgContent(MSG_CONTENT)
		 * .addExtra("from", FROM) .build()) .build();
		 */
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(Audience.all())
				.setNotification(Notification.alert(MSG_CONTENT))
				.setMessage(Message.newBuilder().setMsgContent(MSG_CONTENT).setTitle("title")
						//.addExtra("from", FROM) .addExtra("message extras key", "extrasparam")
						 .build())
				.build();

	}

	//推送调用API
	/**
	 * 广播(所有人)推送:向所有安装该账号指向的手机app客户端发送app推送消息的方法
	 * @param message 推送的数据信息
	 */
	public void push(String message){
	    this.publish(message, FROM);
	}
}
